
package term_project;

import java.util.ArrayList;

public class ComputerUser extends User
{
    protected String password;                                                  // Holds the users passwords
    protected ArrayList<User> friendsList;                                      // Users friends list
    protected ArrayList<Chat> chatList;
    
    private void loadFakeChats()
    {
        Chat fake;
        fake = new Chat(666, "Doug Hogan");
        fake.addUser(new User(12, "Doug"));
        fake.addUser(new User(47, "William"));
        addChat(fake);
        fake = new Chat(789, "Cmpsc 221");
        fake.addUser(new User(13, "Martin"));
        addChat(fake);
        fake = new Chat(999, "PSU");
        fake.addUser(new User(123, "Nittany Lion"));
        addChat(fake);
    }
    
    public void addChat(Chat chat)
    {
        chatList.add(chat);
    }
    
    public ComputerUser()
    // Pre:  User is the desigured log in user name. Password is the for the desired log in
    //       id is the value of the id.
    // Post: All the variables are set.
    {
        super(-1, "");
        password = "";
        friendsList = new ArrayList();
        loadFriendsList();
        loadChatList();
    }
    

    
    public ArrayList<User> getFriendsList()
    {
        return friendsList;
    }
    public ArrayList<Chat> getChatList()
    {
        return chatList;
    }
    public Chat getChat(int index)
    {
        try
        {
            return chatList.get(index);
        }
        catch(Exception e)
        {
            return null;
        }
    }
    public boolean addFriend(String username)
            // PRE:
            // POST: 
    {
        int tempId;
        User tempUser;
        try
        {
            tempId = searchID(username);
            // Call to database to add the user
            tempUser = new User(tempId, username);
            friendsList.add(tempUser);
            return true;
        }
        catch(Exception e)
        {
            System.out.println("Error adding friend");
            return false;
        }
    }
    
    public boolean removeFriend(String username)
    {
        int id;         // Friends id
        User tempUser;
        try
        {
            id = searchID(username);
            tempUser = new User(id, username);
            if(true)        // Call to the database
            {
                for(int i = 0; i < friendsList.size(); i++)
                {
                    if(friendsList.get(i).getId() == id && friendsList.get(i).getUsername().equalsIgnoreCase(username))
                    {
                        friendsList.remove(i);
                        return true;
                    }
                }
            }
            System.out.println("Could not find friend");
            return false;
            
        }
        catch(Exception e)
        {
            System.out.println("Error removing friend");
            return false;
        }
    }
    // ****************************
    // * Loading functions        *
    // ****************************
    private void loadFriendsList()
    // Pre:  Must be in a valid chat room
    // Post: Returns the active users of the list
    {
        
        friendsList = new ArrayList();                                          // Creats a new user arraylist
        
        // TODO: Load active users from the database
       
        // **************************
        // * Temporary user adding  *
        // **************************
        friendsList.add(new User(0, "Admin"));
        friendsList.add(new User(1, "John Smith"));
        
       
    }
    private void loadChatList()
    {
        chatList = new ArrayList();
        // TODO: Connect to database and actually load the chats.
        loadFakeChats();
    }

    ///////////////////////////////
    //     Static Functions      //
    ///////////////////////////////
    
    public boolean logIn(String username, String password)
    {
       
        if(true)    // the sql request was successful
        {
            userID = 2;         // Gets the returned value from the swl request
            this.username = username;
            return true;     // Returns the fetched id
        }
        else
        {
            return false;      // Returns an error
        }
    }
    
    private static int searchID(String username)
    {
        int id;         // Holds the found id
        if(true)
        {
            id =  3;    // This will be the database call to the user
        }
        else
        {
            id = -1;
        }
        return id;
    }
}
